#include "mysettings.h"

#include <QDir>

QString MySettings::appName = "meegim";
QString MySettings::pathMeegIMHome = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName;
QString MySettings::pathMeegIMCache = pathMeegIMHome + QDir::separator() + QString("cache");
QString MySettings::fileConfig = pathMeegIMHome + QDir::separator() + MySettings::appName + ".conf";

MySettings::MySettings(QObject *parent) : QSettings(MySettings::fileConfig, QSettings::NativeFormat , parent)
{
    group_main = "main";
    group_gui = "gui";
    group_xmpp = "xmpp";
    group_accounts = "accounts";

    key_jid = "jid";
    key_passwd = "passwd";
    key_showGroup = "show_groups";
    key_lastPresence = "show_last_presence";
    key_accounts = "accounts";
    key_resource = "resource";
    key_statusmsg = "statusmsg";
    key_default = "is_default";
}

QStringList MySettings::getListAccounts()
{
    beginGroup( group_accounts );
    QVariant ret = value( key_accounts, QStringList() );
    endGroup();
    return ret.toStringList();
}

void MySettings::addAccount( const QString &acc )
{
    beginGroup( group_accounts );
    QVariant retList = value( key_accounts, QStringList() );
    QStringList sl = retList.toStringList();
    if( sl.indexOf(acc) < 0 ) {
        sl.append(acc);
        setValue( key_accounts, QVariant(sl) );
    }
    endGroup();
}

void MySettings::remAccount( const QString &acc )
{
    beginGroup( group_accounts );
    QVariant retList = value( key_accounts, QStringList() );
    QStringList sl = retList.toStringList();
    if( sl.indexOf(acc) < 0 ) {
        sl.removeOne(acc);
        setValue( key_accounts, QVariant(sl) );
    }
    endGroup();
}

/*-------------------*/

QString MySettings::getPasswd(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_passwd, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setPasswd(const QString &jid, const QString &passwd)
{
    beginGroup( jid );
    setValue( key_passwd, QVariant(passwd) );
    endGroup();
}


/*-----------*/


QString MySettings::getResource(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_resource, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setResource(const QString &jid, const QString &resource)
{
    beginGroup( jid );
    setValue( key_resource, QVariant(resource) );
    endGroup();
}


/*-----------*/


bool MySettings::isAccDefault(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_default, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setAccDefault(const QString &jid, const bool &def)
{
    beginGroup( jid );
    setValue( key_default, QVariant(def) );
    endGroup();
}


/*-----------*/
